#include "hplcqinfinepumpparsepacket.h"
#include "hplcqinfinepumpprotocoldata.h"
#include "common/hplcqinfinepfcid.h"
#include "common/hplcqinfinepacketformat.h"

#include <QVariant>
#include "modulehplccmdid.h"

namespace hplc
{
namespace qinfine
{

HplcQinfinePumpParsePacket::HplcQinfinePumpParsePacket(HplcQinfinePumpProtocolData &data, hplc::PumpParam &param, QObject *parent)
    : HplcQinfineParseCommonPacket (data, param, parent)
{

}

HplcQinfinePumpParsePacket::~HplcQinfinePumpParsePacket()
{

}

void HplcQinfinePumpParsePacket::parseSet(quint8 pfc, char *data)
{
    PumpParam *param = (PumpParam*)(m_param);
    switch (pfc)
    {
    case PFC_PUMP_FLOW:
    {
        quint32 temp = PTR2U32(data);
        float flow = *(float*)(&temp);
        param->basicParam.rate = flow;

        QVariant var;
        var.setValue(param->basicParam.rate);
        emit parseCompleted(hplc::PumpCmdID_SetFlow, var);
    }
        break;
    case PFC_PUMP_FLOW_PERCENT:
    {
        param->basicParam.ratePercent = data[0];

        QVariant var;
        var.setValue(param->basicParam.ratePercent);
        emit parseCompleted(hplc::PumpCmdID_SetFlowPercent, var);
    }
        break;
    case PFC_PUMP_PRESS_MIN:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressureMin = press;

        QVariant var;
        var.setValue(param->basicParam.pressureMin);
        emit parseCompleted(hplc::PumpCmdID_SetPressureMin, var);
    }
        break;
    case PFC_PUMP_PRESS_MAX:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressureMax = press;

        QVariant var;
        var.setValue(param->basicParam.pressureMax);
        emit parseCompleted(hplc::PumpCmdID_SetPressureMax, var);
    }
        break;
    case PFC_PUMP_PRESS_WARNING:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressureWarning = press;

        QVariant var;
        var.setValue(param->basicParam.pressureWarning);
        emit parseCompleted(hplc::PumpCmdID_SetPressureWarning, var);
    }
        break;
    case PFC_PUMP_START_STOP:
    {
        param->basicParam.isStart = data[0];

        QVariant var;
        var.setValue(param->basicParam.isStart);
        emit parseCompleted(hplc::PumpCmdID_SetStartStop, var);
    }
        break;
    case PFC_PUMP_PAUSE_RESTORE:
    {
        param->basicParam.isPause = data[0];

        QVariant var;
        var.setValue(param->basicParam.isPause);
        emit parseCompleted(hplc::PumpCmdID_SetPauseRestore, var);
    }
        break;
    case PFC_PUMP_PURGE:
    {
        param->basicParam.isPurge = data[0];

        QVariant var;
        var.setValue(param->basicParam.isPurge);
        emit parseCompleted(hplc::PumpCmdID_SetPurge, var);
    }
        break;
    case PFC_PUMP_PURGE_FLOW:
    {
        quint32 temp = PTR2U32(data);
        float flow = *(float*)(&temp);
        param->basicParam.purgeRate = flow;

        QVariant var;
        var.setValue(param->basicParam.purgeRate);
        emit parseCompleted(hplc::PumpCmdID_SetPurgeFlow, var);
    }
        break;
    case PFC_PUMP_PURGE_TIME:
    {
        param->basicParam.purgeTime = data[0];

        QVariant var;
        var.setValue(param->basicParam.purgeTime);
        emit parseCompleted(hplc::PumpCmdID_SetPurgeTime, var);
    }
        break;
    case PFC_PUMP_PRESS_ZERO:
    {
        quint32 temp = PTR2U32(data);
        float voltage = *(float*)(&temp);
        param->basicParam.pressureZeroVoltage = voltage;

        QVariant var;
        var.setValue(param->basicParam.pressureZeroVoltage);
        emit parseCompleted(hplc::PumpCmdID_SetPressZero, var);
    }
        break;
    case PFC_PUMP_PRESS_UPLOAD_FREQ:
    {
        param->basicParam.pressureUploadFrequency = data[0];

        QVariant var;
        var.setValue(param->basicParam.pressureUploadFrequency);
        emit parseCompleted(hplc::PumpCmdID_SetPressUploadFreq, var);
    }
        break;
    case PFC_PUMP_FLOW_PRESS_COMPEN:
    {
        param->basicParam.isUsedPressureCompensation = data[0];

        QVariant var;
        var.setValue(param->basicParam.isUsedPressureCompensation);
        emit parseCompleted(hplc::PumpCmdID_SetFlowPressCompen, var);
    }
        break;
    case PFC_PUMP_MODE:
    {
        param->basicParam.mode = (PumpMode)data[0];

        QVariant var;
        var.setValue(param->basicParam.mode);
        emit parseCompleted(hplc::PumpCmdID_SetMode, var);
    }
        break;
    case PFC_PUMP_PRESS:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressure = press;

        QVariant var;
        var.setValue(param->basicParam.pressure);
        emit parseCompleted(hplc::PumpCmdID_SetPressure, var);
    }
        break;
    case PFC_PUMP_RUNMODE:
    {
        param->basicParam.runMode = (PumpRunMode)data[0];

        QVariant var;
        var.setValue(param->basicParam.runMode);
        emit parseCompleted(hplc::PumpCmdID_SetRunMode, var);
    }
        break;
    case PFC_PUMP_EXT_SYSTEM:
    {
        HplcQinfinePumpProtocolData *m_d = static_cast<HplcQinfinePumpProtocolData*>(this->m_d);
        m_d->m_systemParser->parseSet(data[0], data + 1);
    }
        break;
    case PFC_PUMP_EXT_USER:
        break;
    default:
        HplcQinfineParseCommonPacket::parseSet(pfc, data);
        break;
    }
}

void HplcQinfinePumpParsePacket::parseGet(quint8 pfc, char *data)
{
    switch (pfc)
    {
    case PFC_PUMP_FLOW:
        break;
    case PFC_PUMP_FLOW_PERCENT:
        break;
    case PFC_PUMP_PRESS_MIN:
        break;
    case PFC_PUMP_PRESS_MAX:
        break;
    case PFC_PUMP_PRESS_WARNING:
        break;
    case PFC_PUMP_START_STOP:
        break;
    case PFC_PUMP_PAUSE_RESTORE:
        break;
    case PFC_PUMP_PURGE:
        break;
    case PFC_PUMP_PURGE_FLOW:
        break;
    case PFC_PUMP_PURGE_TIME:
        break;
    case PFC_PUMP_PRESS_ZERO:
        break;
    case PFC_PUMP_PRESS_UPLOAD_FREQ:
        break;
    case PFC_PUMP_FLOW_PRESS_COMPEN:
        break;
    case PFC_PUMP_MODE:
        break;
    case PFC_PUMP_PRESS:
        break;
    case PFC_PUMP_RUNMODE:
        break;
    case PFC_PUMP_EXT_SYSTEM:
    {
        HplcQinfinePumpProtocolData *m_d = static_cast<HplcQinfinePumpProtocolData*>(this->m_d);
        m_d->m_systemParser->parseGet(data[0], data + 1);
    }
        break;
    case PFC_PUMP_EXT_USER:
        break;
    default:
        HplcQinfineParseCommonPacket::parseGet(pfc, data);
        break;
    }
}


} // qinfine
} // hplc
